using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WindowsApplication1.localhost;
using System.ServiceModel;

namespace WindowsApplication1
{
    public struct User
    {
        public string name;
        public BindingList<string> roles;
    }

    public partial class AdminForm : Form
    {

        private string userName;
        private string password;
        private List<User> UserList;

        public AdminForm(string user,string pass)
        {
            this.userName = user;
            this.password = pass;
            
            InitializeComponent();

            GetStuffFromWebService();
        }

        private void listBoxUserList_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                listBoxRoleList.SelectedIndex = -1;
                int nr = 0;
                foreach (User u in UserList)
                {
                    if (u.name == (string)listBoxUserList.SelectedItem)
                        nr = UserList.IndexOf(u);
                }

                for (int i = 0; i < listBoxRoleList.Items.Count; i++)
                {
                    if (UserList[nr].roles.Contains((string)listBoxRoleList.Items[i]))
                    {
                        listBoxRoleList.SetSelected(i, true);
                    }

                }
            }
            catch (Exception)
            {
                //do nothing
            }

        }

        private void GetStuffFromWebService()
        {

            UserList = new List<User>();
            CalculatorClient client = null;
            try
            {
                client = new CalculatorClient();
                client.ClientCredentials.UserName.UserName = userName;
                client.ClientCredentials.UserName.Password = password;

                BindingList<string> users = client.GetAllUsers();
                BindingList<string> roles = client.GetAllRoles();

                listBoxUserList.Items.Clear();
                listBoxRoleList.Items.Clear();
                comboBox1.Items.Clear();
                comboBox2.Items.Clear();

                foreach (string s in users)
                {
                    listBoxUserList.Items.Add(s);
                    comboBox1.Items.Add(s);

                    User u = new User();
                    u.name = s;
                    u.roles = client.GetUserRoles(s);
                    UserList.Add(u);
                }
                foreach (string s in roles)
                {
                    listBoxRoleList.Items.Add(s);
                    comboBox2.Items.Add(s);
                }

                client.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Exception: " + ex.Message);

                if (ex.InnerException != null)
                    MessageBox.Show("Inner Exception: " + ex.InnerException.Message);
            }
            finally
            {
                if (client.State == CommunicationState.Opened)
                    client.Close();
            }
        }

        private void buttonGetUsers_Click(object sender, EventArgs e)
        {
            GetStuffFromWebService();
        }

        private void buttonAddUser_Click(object sender, EventArgs e)
        {
            CalculatorClient client = null;
            try
            {
                client = new CalculatorClient();
                client.ClientCredentials.UserName.UserName = userName;
                client.ClientCredentials.UserName.Password = password;

                if (textBoxUsrNameAdd.Text != "" || maskedTextBoxPWDUsrAdd.Text != "" || maskedTextBoxRePWDUsrAdd.Text != "" || textBoxEmail.Text != "")
                {
                    if (maskedTextBoxPWDUsrAdd.Text == maskedTextBoxRePWDUsrAdd.Text)
                    {
                        string operationResult = client.AddUser(textBoxUsrNameAdd.Text, maskedTextBoxRePWDUsrAdd.Text, textBoxEmail.Text);
                        MessageBox.Show("Result of adding new user : " + operationResult);
                        if (operationResult == "Sucess")
                        {
                            textBoxUsrNameAdd.Clear();
                            maskedTextBoxPWDUsrAdd.Clear();
                            maskedTextBoxRePWDUsrAdd.Clear();
                            textBoxEmail.Clear();
                        }
                    }
                    else
                        MessageBox.Show("The given passwords don't match!!");
                }
                else
                    MessageBox.Show("Please give user name,email and twice the password");

                client.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Exception: " + ex.Message);

                if (ex.InnerException != null)
                    MessageBox.Show("Inner Exception: " + ex.InnerException.Message);
            }
            finally
            {
                if (client.State == CommunicationState.Opened)
                    client.Close();
            }
        }

        private void buttonAddUserToRole_Click(object sender, EventArgs e)
        {
            CalculatorClient client = null;
            try
            {
                client = new CalculatorClient();
                client.ClientCredentials.UserName.UserName = userName;
                client.ClientCredentials.UserName.Password = password;

                if (comboBox1.SelectedIndex != -1 && comboBox2.SelectedIndex != -1)
                {
                    MessageBox.Show("Was the operation was succesful? : " + client.AddUserToRole(comboBox1.SelectedItem.ToString(), comboBox2.SelectedItem.ToString()));
                }

                client.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Exception: " + ex.Message);

                if (ex.InnerException != null)
                    MessageBox.Show("Inner Exception: " + ex.InnerException.Message);
            }
            finally
            {
                if (client.State == CommunicationState.Opened)
                    client.Close();
            }       
        }

        private void buttonRemoveUser_Click(object sender, EventArgs e)
        {
            CalculatorClient client = null;
            try
            {
                client = new CalculatorClient();
                client.ClientCredentials.UserName.UserName = userName;
                client.ClientCredentials.UserName.Password = password;

                if (listBoxUserList.SelectedIndex != -1)
                {
                    MessageBox.Show("Was the operation was succesful? : " + client.RemoveUser(listBoxUserList.SelectedItem.ToString()));
                }

                client.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Exception: " + ex.Message);

                if (ex.InnerException != null)
                    MessageBox.Show("Inner Exception: " + ex.InnerException.Message);
            }
            finally
            {
                if (client.State == CommunicationState.Opened)
                    client.Close();
            }       
        }
        
    }
}